package com.symaster.common;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author yinmiao
 * @version 2022-12-19 18:52
 */
public class ExceptionFactory {

    private static Class<? extends RuntimeException> customizeException = RuntimeException.class;

    public static void setCustomizeException(Class<? extends RuntimeException> customizeException) {
        ExceptionFactory.customizeException = customizeException;
    }

    public static Class<? extends RuntimeException> getCustomizeException() {
        return customizeException;
    }

    public static RuntimeException getDefaultException(String errorMessage) {
        try {
            if (customizeException == null) {
                throw new NullPointerException();
            }
            Constructor<? extends RuntimeException> constructor = customizeException.getConstructor(String.class);
            return constructor.newInstance(errorMessage);
        } catch (NoSuchMethodException |
                 InvocationTargetException |
                 IllegalAccessException |
                 InstantiationException e) {

            throw new RuntimeException(e);
        }
    }
}
